package com.zhu.storesmanager.views;

import com.zhu.storesmanager.R;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 编辑文本查看弹窗界面
 */
public class PopEditText {

    private FrameLayout mContentView = null; //父View
    private View mPopView = null; //谈窗界面
    private TextView mTextView = null;
    private EditText mEditView = null;

    /**
     * @param layout 需要添加文本弹窗的父界面
     * 
     */
    public PopEditText(FrameLayout layout){
        mContentView = layout;
        mPopView = LayoutInflater.from(mContentView.getContext()).inflate(R.layout.layout_popdialog_input, null);
        mPopView.setOnClickListener(mListener);
        ImageView close = (ImageView) mPopView.findViewById(R.id.checkpop_close);
        close.setVisibility(View.GONE);
        mPopView.findViewById(R.id.checkpop_button_ok).setOnClickListener(mListener);
        mPopView.findViewById(R.id.checkpop_button_cancel).setOnClickListener(mListener);
        mEditView = (EditText) mPopView.findViewById(R.id.checkpop_text);
    }

    /**
     * 显示弹窗界面
     * @param title 标题
     * @param textView 内容界面
     */
    public void showPopEditText(String title, TextView textView){
        mTextView = textView;
        TextView titleView = (TextView) mPopView.findViewById(R.id.checkpop_title);
        titleView.setText(title);
        mEditView.setText(textView.getText());
        if(mContentView.indexOfChild(mPopView) == -1){
            mContentView.addView(mPopView);
        }
    }

    /**
     * 显示弹窗界面
     * @param titleId 标题id
     * @param textView 内容界面
     */
    public void showPopEditText(int titleId, TextView textView){
        showPopEditText(mContentView.getContext().getString(titleId), textView);
    }

    /**
     * 关闭弹窗界面
     */
    public void closePopEditText(){
        if(mContentView.indexOfChild(mPopView) != -1){
            mContentView.removeView(mPopView);
        }
    }

    /**
     * 处理返回键
     * @return true 表示截获返回键处理弹窗消息
     */
    public boolean doBack(){
        if(mContentView.indexOfChild(mPopView) != -1){
            mContentView.removeView(mPopView);
            return true;
        }
        return false;
    }

    /**
     * 点击时间监听
     */
    private OnClickListener mListener = new OnClickListener(){
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v != null){
                if(v.getId() == R.id.checkpop_button_cancel){ //取消
                    closePopEditText();
                }else if(v.getId() == R.id.checkpop_button_ok){ //确定
                    mTextView.setText(mEditView.getText().toString());
                    closePopEditText();
                }
            }
        }
    };
}
